package com.yuanian.springbootProject.controller;

import com.yuanian.springbootProject.dto.ResponseResult;
import com.yuanian.springbootProject.entity.TRole;
import com.yuanian.springbootProject.service.IRoleService;
import com.yuanian.springbootProject.util.Utils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;

@RestController
@RequestMapping("role")
public class RoleController {

    @Resource
    private IRoleService roleService;

    /**
     * 插入一个角色
     * @param role 角色数据
     * @return
     */
    @PostMapping("one")
    public ResponseResult insertOne(@RequestBody TRole role){
        role.setRoleId(Utils.getRandomUUID());
        role.setCreateDate(new Date());
        role.setUpdateDate(new Date());
        int i = roleService.insert(role);
        if (i > 0) return ResponseResult.ok();
        else return ResponseResult.fail();
    }

    /**
     * 根据id删除一个角色
     * @param id
     * @return
     */
    @DeleteMapping("one/{id}")
    public ResponseResult deleteByID(@PathVariable String id){
        if (roleService.deleteRoleAndRelationByPrimaryKey(id)>0) return ResponseResult.ok();
        else return ResponseResult.fail();
    }

    /**
     * 更新一个角色
     * @param role
     * @return
     */
    @PutMapping("one")
    public ResponseResult updateOne(@RequestBody TRole role){
        if (roleService.updateByPrimaryKeySelective(role)>0) return ResponseResult.ok();
        else return ResponseResult.fail();
    }

    /**
     * 根据id查找一个角色
     * @param id
     * @return
     */
    @GetMapping("one/{id}")
    public ResponseResult getOneById(@PathVariable("id") String id){
        return ResponseResult.ok(roleService.selectByPrimaryKey(id));
    }

    /**
     * 查找所有角色
     * @return
     */
    @GetMapping("all")
    public ResponseResult getAll(){
        return ResponseResult.ok(roleService.selectAll());
    }

}
